package com.thm.tfast.modules.sys.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.thm.tfast.core.exception.Asserts;
import com.thm.tfast.core.sevruity.domain.SysUserDetails;
import com.thm.tfast.core.utils.SecurityUtil;
import com.thm.tfast.modules.sys.mapper.SysResourceCategoryMapper;
import com.thm.tfast.modules.sys.model.SysResource;
import com.thm.tfast.modules.sys.model.SysResourceCategory;
import com.thm.tfast.modules.sys.model.dto.resource.ResourceCategoryInsertDto;
import com.thm.tfast.modules.sys.model.dto.resource.ResourceCategoryUpdateDto;
import com.thm.tfast.modules.sys.service.SysResourceCategoryService;
import com.thm.tfast.modules.sys.service.SysResourceService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * <p>
 * 资源类别信息 服务实现类
 * </p>
 *
 * @author THM
 * @since 2022-10-29
 */
@Service
public class SysResourceCategoryServiceImpl extends ServiceImpl<SysResourceCategoryMapper, SysResourceCategory> implements SysResourceCategoryService {

    @Resource
    private SysResourceService sysResourceService;

    @Override
    public Page<SysResourceCategory> page(String keyword, Integer pageNum, Integer pageSize) {
        Page<SysResourceCategory> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SysResourceCategory> wrapper = Wrappers.lambdaQuery();
        if (StrUtil.isNotEmpty(keyword)) {
            wrapper.like(SysResourceCategory::getName, keyword);
        }
        wrapper.orderByAsc(SysResourceCategory::getSort);
        return this.page(page, wrapper);
    }

    @Override
    @Transactional
    public Long insert(ResourceCategoryInsertDto dto) {
        SysResourceCategory sysResourceCategory = this.getOne(Wrappers
                .<SysResourceCategory>lambdaQuery()
                .eq(SysResourceCategory::getName, dto.getName()));
        if (Objects.nonNull(sysResourceCategory)) {
            Asserts.fail("资源类型已经存在");
        }
        sysResourceCategory = BeanUtil.toBean(dto, SysResourceCategory.class);
        SysUserDetails currentUser = SecurityUtil.getCurrentUser();
        sysResourceCategory.setCreateUser(currentUser.getUserId());
        sysResourceCategory.setCreateTime(currentUser.getDate());
        sysResourceCategory.setUpdateUser(currentUser.getUserId());
        sysResourceCategory.setUpdateTime(currentUser.getDate());
        this.save(sysResourceCategory);
        return sysResourceCategory.getId();
    }

    @Override
    @Transactional
    public Boolean update(ResourceCategoryUpdateDto dto) {
        SysResourceCategory sysResourceCategory = this.getById(dto.getId());
        if (Objects.isNull(sysResourceCategory)) {
            Asserts.fail("资源类型不存在");
        }
        sysResourceCategory = BeanUtil.toBean(dto, SysResourceCategory.class);
        SysUserDetails currentUser = SecurityUtil.getCurrentUser();
        sysResourceCategory.setUpdateUser(currentUser.getUserId());
        sysResourceCategory.setUpdateTime(currentUser.getDate());
        return this.updateById(sysResourceCategory);
    }

    @Override
    @Transactional
    public Boolean delete(Long categoryId) {
        if (Objects.isNull(this.getById(categoryId))) {
            Asserts.fail("资源类型不存在");
        }
        int count = sysResourceService.count(Wrappers
                .<SysResource>lambdaQuery()
                .eq(SysResource::getCategoryId,categoryId));
        if (count != 0){
            Asserts.fail("资源类型已绑定资源不能删除");
        }
        return this.removeById(categoryId);
    }
}
